_WindowJS = function() {};
_WindowJS.prototype.current_object = null;
_WindowJS.prototype.current_event = null;
_WindowJS.prototype.x = null;
_WindowJS.prototype.y = null;
_WindowJS.prototype.z = 1000;
_WindowJS.prototype.window_count = 0;

_WindowJS.prototype.close_window = function (options) {
	var body = document.getElementsByTagName ('body')[0];
	body.removeChild (document.getElementById (options));
}
_WindowJS.prototype.create_window = function (options) {
	var body = document.getElementsByTagName ('body')[0];

	window_id = options.window_id + '_' + WindowJS.window_count;
	body.innerHTML += '\
<div id="'+window_id+'" class="css_window">\
	<div class="css_window_title_bar">\
		'+options.title+'\
		<div class="css_window_control">\
			<a href="javascript:WindowJS.close_window (\''+window_id+'\');">Close</a>\
			<a href="javascript:WindowJS.close_window (\''+window_id+'\');">Min</a>\
			<a href="javascript:WindowJS.close_window (\''+window_id+'\');">Max</a>\
		</div>\
	</div>\
	<div class="css_window_panel">\
		<div class="css_window_panel_view">\
		'+options.content+'\
		</div>\
	</div>\
</div>';

	WindowJS.window_count++;
}






























WindowJS = new _WindowJS ();

var _onmousedown = window.onmousedown ? window.onmousedown : null;
onmousedown = function (e) {

	_onmousedown ? _onmousedown (e) : null;

	var e = e ? e : window.event;
	WindowJS.current_object = e.target ? e.target.parentNode : window.event.srcElement.parentNode;
	var coords = mouseCoords (e);
	if (WindowJS.current_object &&
		WindowJS.current_object.className == 'css_window') 
		WindowJS.current_object.style.zIndex = (WindowJS.z++);
	WindowJS.ox = coords.ox;
	WindowJS.oy = coords.oy;
}

var _onmouseup = window.onmouseup ? window.onmouseup : null;
onmouseup = function (e) {

	_onmouseup ? _onmouseup (e) : null;
	var e = e ? e : window.event;
	WindowJS.current_object = null;
}
/* var _onmouseover = window.onmouseover ? window.onmouseover : null;
onmouseover = function (e) {

	var e = e ? e : window.event;
	WindowJS.current_object = e.target ? e.target.parentNode : window.event.srcElement.parentNode;
	WindowJS.current_event = e;
	var coords = mouseCoords (e);
	WindowJS.ox = coords.ox;
	WindowJS.oy = coords.oy;
} */
/*var _onmouseout = window.onmouseout ? window.onmouseout : null;
onmouseout = function (e) {

	_onmouseout ? _onmouseout (e) : null;
	var e = e ? e : window.event;
	WindowJS.current_object = null;
} */
var _onmousemove = window.onmousemove ? window.onmousemove : null;
onmousemove = function (e) {

	_onmousemove ? _onmousemove (e) : null;

	var e = e ? e : window.event;
	var coords = mouseCoords (e);
	WindowJS.x = coords.x;
	WindowJS.y = coords.y;
	if (WindowJS.current_object && WindowJS.current_object.className != 'css_window') return;
	if (!WindowJS.current_object) {
		return;
	}

	WindowJS.current_object.style.left = ((coords.x - WindowJS.ox)+'px');
	WindowJS.current_object.style.top = ((coords.y - WindowJS.oy)+'px');

	window.status = 'x='+coords.x+',y='+coords.y+',ox='+coords.ox+',oy='+coords.oy+',target.top='+WindowJS.current_object.offsetTop+',target.left='+WindowJS.current_object.offsetLeft;
}

function mouseCoords (ev) {
	ev = ev ? ev : window.event;
	if (ev.pageX || ev.pageY) return {x:ev.pageX, y:ev.pageY,
			ox:ev.layerX, oy:ev.layerY,
			target:ev.target
		};
	else return {

		x:ev.clientX + document.body.scrollLeft -
				document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop -
				document.body.clientTop,
		target:ev.srcElement
	};
}
